package com.wtone.platform.gat.images.gat.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.google.gson.Gson;
import com.wtone.platform.gat.images.gat.core.R;
import com.wtone.platform.gat.images.gat.core.Response;
import com.wtone.platform.gat.images.gat.model.RegisterObject;
import com.wtone.platform.gat.images.gat.utils.DigestHttpClientUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: aiLin
 * @Date: 2020/9/27 16:21
 * @desc:  Digests 服务端接口
 */
@Api(tags = "Digests 服务端", value = "Digests api")
@ApiSort(3)
@RestController
@Slf4j
public class DigestsController {

    private static final String PARAM = "";
    private static final String USERNAME = "wtw";
    private static final String PASSWORD = "wtw12345+";

    @RequestMapping(value = { "/test" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
    public R test(HttpServletResponse rsp){
        return R.data("hello,你好我是Digests 服务端!欢迎你！");
    }


    @RequestMapping(value = { "/VIID/System/Register/hk" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
    public JSONObject register(@RequestBody RegisterObject registerObject){
        Gson gson = new Gson();
        String params = gson.toJson(Response.object2UpperCase(registerObject));  //转换json数据
        log.info("注册参数：{}",params);
        //String result = HttpRequestUtils.sendPost("http://190.35.205.17:8120/VIID/System/Register", PARAM, USERNAME, PASSWORD, params, "json","44030520205039102815");
        String s = DigestHttpClientUtil.sendPostByJson("http://190.35.205.17:8120/VIID/System/Register", "{\n" +
                "\t\"RegisterObject\": {\n" +
                "\t\t\"DeviceID\": 44030520205039102815\n" +
                "\t}\n" +
                "}", 0,USERNAME,PASSWORD);
        JSONObject jo = JSONObject.parseObject(s);
        return jo;
    }

    @RequestMapping(value = { "/VIID/System/Keepalive/hk" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
    public JSONObject keepalive(@RequestBody RegisterObject registerObject){
        Gson gson = new Gson();
        String params = gson.toJson(Response.object2UpperCase(registerObject));  //转换json数据
        String s = DigestHttpClientUtil.sendPostByJson("http://190.35.205.17:8120/VIID/System/Keepalive", "{ \n" +
                "\"KeepaliveObject\": { \n" +
                "\"DeviceID\": \"44030520205039102815\" \n" +
                "} \n" +
                "}", 0,USERNAME,PASSWORD);
        JSONObject jo = JSONObject.parseObject(s);
        return jo;
    }

    @RequestMapping(value = { "/VIID/System/UnRegister/hk" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
    public JSONObject unRegister(@RequestBody RegisterObject registerObject){
        Gson gson = new Gson();
        String params = gson.toJson(Response.object2UpperCase(registerObject));  //转换json数据
        String s = DigestHttpClientUtil.sendPostByJson("http://190.35.205.17:8120/VIID/System/UnRegister", "{\n" +
                "\"UnRegisterObject\":{\n" +
                "\"DeviceID\":\"44030520205039102815\"\n" +
                "}", 0,USERNAME,PASSWORD);
        JSONObject jo = JSONObject.parseObject(s);
        return jo;
    }
}
